/**
 * @author harry
 * @date 2022/3/14/20:37
 */

package com.serviceOrchestration.atomic.units;

import com.serviceOrchestration.annotation.AtomicService;
import com.serviceOrchestration.atomic.AtomicUnit;
import com.serviceOrchestration.atomic.AtomicUnitContext;
import com.serviceOrchestration.factory.AtomicFactory;

import java.text.SimpleDateFormat;
import java.util.Calendar;

@AtomicService(
        value = AtomicFactory.BEAN_PREFIX + 7,
        name = "利息计算服务",
        description = "根据用户购买金额和产品属性计算预期利息"
)
public class InterestComputeUnit extends AtomicUnit {

    @Override
    protected boolean doHandle(AtomicUnitContext context) {
        int amount = (int) context.getContext("amount");
        int period = (int) context.getContext("period");
        int annualRate = (int) context.getContext("annualRate");
        Calendar due = Calendar.getInstance();
        due.add(Calendar.DAY_OF_YEAR, period);
        double interest = amount * annualRate / 10000.0 * period / 360.0;
        context.addReturnData("interest", interest);
        context.addReturnData("due", new SimpleDateFormat("yyyy-MM-dd").format(due.getTime()));
        return true;
    }
}
